/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
/**
 * 
 */
package com.genia.toolbox.persistence.memory.criteria.field;

import java.util.List;

import com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult;
import com.genia.toolbox.persistence.exception.AggregateException;
import com.genia.toolbox.persistence.exception.FieldDefinitionException;

/**
 * TRhis field represent an arithmetic operation between two fields.
 */
public class MemoryArithmeticOperationField
    extends AbstractMemoryField
{

  /**
   * this enum contains all the operations available.
   */
  public enum ArithmeticOperation
  {
    /**
     * the addition.
     */
    ADD
    {
      /**
       * return the result of the arithmetoc operation over two values.
       * 
       * @param value1
       *          the first operand
       * @param value2
       *          the second operand
       * @return the result of the operation
       */
      @Override
      protected double operate(final double value1, final double value2)
      {
        return value1 + value2;
      }
    },
    /**
     * the substraction.
     */
    SUB
    {
      /**
       * return the result of the arithmetoc operation over two values.
       * 
       * @param value1
       *          the first operand
       * @param value2
       *          the second operand
       * @return the result of the operation
       */
      @Override
      protected double operate(final double value1, final double value2)
      {
        return value1 - value2;
      }
    },
    /**
     * the multiplication.
     */
    MULT
    {
      /**
       * return the result of the arithmetoc operation over two values.
       * 
       * @param value1
       *          the first operand
       * @param value2
       *          the second operand
       * @return the result of the operation
       */
      @Override
      protected double operate(final double value1, final double value2)
      {
        return value1 * value2;
      }
    },
    /**
     * the division.
     */
    DIV
    {
      /**
       * return the result of the arithmetoc operation over two values.
       * 
       * @param value1
       *          the first operand
       * @param value2
       *          the second operand
       * @return the result of the operation
       */
      @Override
      protected double operate(final double value1, final double value2)
      {
        return value1 / value2;
      }
    };

    /**
     * return the result of the arithmetoc operation over two values.
     * 
     * @param value1
     *          the first operand
     * @param value2
     *          the second operand
     * @return the result of the operation
     */
    protected abstract double operate(double value1, double value2);
  }

  /**
   * the arithmetic operation of this field.
   */
  private final transient ArithmeticOperation operation;

  /**
   * the first operand.
   */
  private final transient AbstractMemoryField field1;

  /**
   * the second operand.
   */
  private final transient AbstractMemoryField field2;



  /**
   * the default constructor of this field.
   * 
   * @param operation
   *          the arithmetic operation of this field
   * @param field1
   *          the first operand
   * @param field2
   *          the second operand
   */
  public MemoryArithmeticOperationField(final ArithmeticOperation operation, final AbstractMemoryField field1, final AbstractMemoryField field2)
  {
    this.operation = operation;
    this.field1 = field1;
    this.field2 = field2;
  }



  /**
   * Return the value of this field for the given line of result.
   * 
   * @param lines
   *          the line of result we compute the field on
   * @return the value of this field for the given line of result
   * @throws FieldDefinitionException
   *           when there is an error in the definition of the field
   * @throws AggregateException
   *           when there is an error with an aggregate definition
   */
  @Override
  public Double getValue(final List<BasicCriteriaResult<?>> lines)
      throws FieldDefinitionException, AggregateException
  {
    final Number number1 = (Number) field1.getValue(lines);
    final Number number2 = (Number) field2.getValue(lines);

    if (number1 == null || number2 == null) {
      return null;
    }

    return operation.operate(number1.doubleValue(), number2.doubleValue());
  }



  /**
   * returns if this <code>Field</code> is an aggregate.
   * 
   * @return <code>true</code> if this <code>Field</code> is an aggregate,
   *         <code>false</code> otherwise.
   */
  public boolean isAggregate()
  {
    return field1.isAggregate() || field2.isAggregate();
  }

}
